package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.dto.PermissionGroupDto;
import com.anxin.registerManagement.bean.jo.PermissionGroupSearchJo;
import com.anxin.registerManagement.bean.persist.PermissionGroup;
import com.anxin.registerManagement.service.PermissionGroupService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 叶前呈
 * @date 2022/1/31 13:42
 */
@Api(tags = "权限组接口")
@RestController
@RequestMapping("/permissiongroups")
public class PermissionGroupController {

    private static final Logger logger = LoggerFactory.getLogger(PermissionGroupController.class);

    @Autowired
    PermissionGroupService permissionGroupService;

    @ApiOperation("查询权限组")
    @PostMapping("/search")
    public ResultUtil getPermissionGroupList(@RequestBody PermissionGroupSearchJo permissionGroupSearchJo) {
        logger.info("查询条件：{}", permissionGroupSearchJo);
        return ResultUtil.success(permissionGroupService.getPermissionGroupList(permissionGroupSearchJo));
    }

    @ApiOperation("添加权限组")
    @PostMapping("/add")
    public ResultUtil addPermissionGroup(@Valid @RequestBody PermissionGroupDto permissionGroupDto) {
        permissionGroupService.addPermissionGroup(permissionGroupDto);
        return ResultUtil.success();
    }

    @ApiOperation("修改权限组")
    @PostMapping("/update")
    public ResultUtil updatePermissionGroup(@Valid @RequestBody PermissionGroupDto permissionGroupDto) {
        Assert.notNull(permissionGroupDto.getId(), "权限组id不能为空");
        permissionGroupService.updatePermissionGroup(permissionGroupDto);
        return ResultUtil.success();
    }

    @ApiOperation("删除权限组")
    @PostMapping("/delete")
    public ResultUtil deletePermissionGroup(@RequestParam("id") List<Integer> id) {
        permissionGroupService.deletePermissionGroup(id);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有权限组")
@GetMapping("/get")
    public ResultUtil getAllPermissionGroup(){
        return ResultUtil.success(permissionGroupService.getAllPermissionGroup());
    }
}
